export default class extends think.Service {
    constructor() {
        super();
        const db = this.db;
        this.postModel = db['sys_post'];
        this.userModel = db['sys_user'];
        this.postUserModel = db['sys_user_post'];  
    }

    /**
     * 查询岗位列表信息
     * @data
     * post_name --岗位名称
     * post_code --岗位编码
     * --------以上为条件查询--------
     * options --分页查询条件
     * options.page --页码 0开始
     * opations.pageSize --单页记录数
     * @return
     * flag --标识
     * count --记录总数
     * rows --当前页数据
     */
    async getPostList(data) {
        data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
        const Op = this.db.Sequelize.Op;
        let where = {};
        if (data.post_name) {
            where.post_name = {
                [Op.like]: '%' + data.post_name + '%'
            }
        }
        try {
            const condition = {
                where: where,
                limit: parseInt(data.pageSize),
                offset: parseInt(data.start),
                order: [
                    ['post_sort','asc']
                ]
            };
            return await this.postModel.findAndCountAll(condition);
        } catch (err) {
            return this.exceptionService.handleError(err);
        }
    }
    /**
     * 保存岗位信息 新增|编辑
     * @data 岗位信息
     * @returns Boolean
     */
    async savePostInfo(data) {
        const Op = this.db.Sequelize.Op;
        try {
            const oldPost = await this.postModel.findOne({
                where: {
                    [Op.or]:{
                        post_name: data.post_name,
                        post_code:data.post_code
                    }
                },
                attributes: ['id', 'post_name','post_code']
            });
            if (data.id) {
                if (oldPost && oldPost.id !== data.id) {
                    return false;
                } else {
                    return await this.postModel.update(data, {
                        where: {
                            id: data.id
                        }
                    });
                }
            } else {
                if (oldPost) {
                    return false;
                } else {
                    data.id = think.uuid('v1');
                    return await this.postModel.create(data);
                }
            }

        } catch (err) {
            return this.exceptionService.handleError(err);
        }
    }

    /**
     * 删除岗位信息
     * @data
     * data.id --删除岗位id
     */
    async delPostInfo(data) {
        const Op = this.db.Sequelize.Op;
        const where = { id: data.id};
        try {
            return await this.postModel.destroy({where: where});
        } catch (err) {
            return this.exceptionService.handleError(err);
        }
    }

    async getAllPost(){
        try {
            return await this.postModel.findAll({
                attributes: ['id','post_code','post_name'], 
                order: [
                    ['post_sort','asc']
                ]
            });
        } catch (err) {
            return this.exceptionService.handleError(err);
        }
    }

    async saveUserPost(data){
        const Op = this.db.Sequelize.Op;
        try {
            if(data.delList.length>0){
                await this.postUserModel.destroy({where:{user_id:data.user_id}});
            }
            if(data.addList.length>0){
                const addData = data.addList.map((item)=>{
                    const param = {
                        user_id:data.user_id,
                        post_id:item
                    };
                    return param;
                })
                await this.postUserModel.bulkCreate(addData);
            }
            return true;
        } catch (err) {
            return this.exceptionService.handleError(err);
        } 
    }

}